
#include <util/common_macros.h>

#include "matrix.h"
/* ========================================================================== */
static boolean __bin_matrix_destroy(ImpObject * object) {
	ImpMatrix * Mat;
	Mat = IMP_MATRIX(object);

	if (Mat->data) mem_free(Mat->data);

	return TRUE;
}
static boolean __bin_matrix_copy(ImpObject * copy, ImpObject * object) {

	ImpMatrix * matrix = IMP_MATRIX(object);
	ImpMatrix * matrix_copy = IMP_MATRIX(copy);

	matrix_copy->rows = matrix->rows;
	matrix_copy->columns = matrix->columns;

	mem_copy((char *)matrix->data, (char *)matrix_copy->data, matrix->size );

	return TRUE;
}
static void __matrix_class_init(ImpObjectClass * klass) {

	klass->object_attributes = NULL;
	klass->object_clone = NULL;
	klass->object_destroy = __bin_matrix_destroy;
	klass->object_copy = __bin_matrix_copy;

	//ImpArrayClass * array_klass;
	//array_klass = IMP_ARRAY_CLASS(klass);
	/* do nothing */
}
static void __matrix_init(ImpObject * object) {
	ImpMatrix * Mat;
	Mat = IMP_MATRIX(object);

	Mat->data = NULL;

}
/* ========================================================================== */
ImpObjectClass * imp_matrix_get_class(void) {
	static ImpMatrixClass * klass = NULL;

	if (klass == NULL) {
		ImpObjectClassInfo info = {
			"ImpMatrix",
			sizeof (ImpMatrix),
			sizeof (ImpMatrixClass),
			(ImpObjectClassInitFunc) __matrix_class_init,
			(ImpObjectInitFunc) __matrix_init,
			(ImpArgSetFunc) NULL,
			(ImpArgGetFunc) NULL
		};

		ImpObjectClass * matrix_class = imp_object_class_new(IMP_OBJECT_TYPE, &info);
		klass = IMP_MATRIX_CLASS(matrix_class);
	}
	return IMP_OBJECT_CLASS(klass);
}
/* ========================================================================== */
boolean imp_matrix_set_data(ImpMatrix * Mat, void * data, int rows, int columns) {
	Mat->rows = rows;
	Mat->columns = columns;
	Mat->data = data;

	return TRUE;
}
boolean imp_matrix_constructor(ImpMatrix * Mat, int rows, int columns) {

	Mat->rows = rows;
	Mat->columns = columns;
	Mat->size = rows * columns;

	Mat->data = mem_allocn(rows * columns, real);

	if (Mat->data) return TRUE;
	else return FALSE;

}
/* ========================================================================== */
ImpMatrix * imp_matrix_new(int rows, int columns) {
	ImpMatrix * Mat;

	Mat = IMP_MATRIX(imp_object_new(IMP_OBJECT_CLASS(imp_matrix_get_class())));

	if (imp_matrix_constructor(Mat, rows, columns)) return Mat;

	imp_object_delete(Mat);
	return NULL;
}